import pymysql

class KifaPipeline:

    conn = None
    cur = None
    # 重写父类，该方法只会在爬虫开始时进行一次
    def open_spider(self,spider):
        print('mysql开始爬虫')
        self.conn = pymysql.Connect(host="172.16.10.187",
                                    user='tidb',
                                    password='Tidb@guest@123',
                                    database='crawl',
                                    port=3306,
                                    charset='utf8')

    # 有几个item对象，就会调用几次
    def process_item(self, item, spider):
        k = ''
        v = ''
        for key, val in item.items():
            k = k + key + ","
            v = v + '"' + val + '"' + ','

        self.cur = self.conn.cursor()
        try:
            self.cur.execute(f'insert into flower_trading_copy1 ({k[:-1]}) values({v[:-1]})')
            self.conn.commit()
        except Exception as e:
            print(e)
            self.conn.rollback()
        return item #该item会传递给下一个管道类

    # 重写父类，该方法只会在爬虫结束时进行一次
    def close_spider(self,spider):
        print('mysql结束爬虫')
        self.conn.close()